package com.xdra.hub.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xdra.hub.api.DashboardApi;
import com.xdra.hub.model.CellMetricsResponse;
import com.xdra.hub.model.ContainerPackMetricsResponse;
import com.xdra.hub.model.Overview;
import com.xdra.hub.model.PackMetricsResponse;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.time.OffsetDateTime;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;



@RestController
@RequestMapping("api/v1")
public class DashboardController implements DashboardApi {

    @Autowired
    private ResourceLoader resourceLoader;

    @Override
    public ResponseEntity<Resource> exportData(OffsetDateTime dateFrom, OffsetDateTime dateTo) {
        return ResponseEntity.ok(resourceLoader.getResource("classpath:test.csv"));
    }

    @Override
    @SneakyThrows
    public ResponseEntity<CellMetricsResponse> getCellMetrics(String containerId, String packId, String cellId, OffsetDateTime dateFrom, OffsetDateTime dateTo) {
        
      InputStream inputStream = getClass().getClassLoader().getResourceAsStream("nydata.json");
String json = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
        CellMetricsResponse response = new ObjectMapper().registerModule(new JavaTimeModule()).readValue(json, CellMetricsResponse.class);
        return ResponseEntity.ok(response);
    }

    @Override
    @SneakyThrows
  public ResponseEntity<ContainerPackMetricsResponse> getContainerPackMetrics(String containerId) {
        ContainerPackMetricsResponse response = new ObjectMapper().readValue("""
                {
                    "packHistograms": [
                        {
                            "packId": "1",
                            "packHistogram": [
                                {
                                    "maxAbsImpedance": 6.027456183070403,
                                    "absImpedanceMean": 0.8008281904610115,
                                    "minAbsImpedance": 1.4658129805029452,
                                    "impedanceStdDev": 5.962133916683182
                                },
                                {
                                    "maxAbsImpedance": 6.027456183070403,
                                    "absImpedanceMean": 0.8008281904610115,
                                    "minAbsImpedance": 1.4658129805029452,
                                    "impedanceStdDev": 5.962133916683182
                                }
                            ]
                        },
                        {
                            "packId": "2",
                            "packHistogram": [
                                {
                                    "maxAbsImpedance": 6.027456183070403,
                                    "absImpedanceMean": 0.8008281904610115,
                                    "minAbsImpedance": 1.4658129805029452,
                                    "impedanceStdDev": 5.962133916683182
                                },
                                {
                                    "maxAbsImpedance": 6.027456183070403,
                                    "absImpedanceMean": 0.8008281904610115,
                                    "minAbsImpedance": 1.4658129805029452,
                                    "impedanceStdDev": 5.962133916683182
                                }
                            ]
                        }
                    ]
                }
                """, ContainerPackMetricsResponse.class);
        return ResponseEntity.ok(response);
    }


    @Override
    @SneakyThrows
    public ResponseEntity<PackMetricsResponse> getPackMetrics(String containerId, String packId) {
        PackMetricsResponse response = new ObjectMapper().readValue("""
                {
                    "packHistogram": [
                        {
                            "maxAbsImpedance": 6.027456183070403,
                            "absImpedanceMean": 0.8008281904610115,
                            "minAbsImpedance": 1.4658129805029452,
                            "impedanceStdDev": 5.962133916683182
                        },
                        {
                            "maxAbsImpedance": 6.027456183070403,
                            "absImpedanceMean": 0.8008281904610115,
                            "minAbsImpedance": 1.4658129805029452,
                            "impedanceStdDev": 5.962133916683182
                        }
                    ],
                    "suggestion": "suggestion",
                    "metrics": {
                        "degradationLevel": "degradationLevel",
                        "absImpedanceMean": 0.8008281904610115,
                        "impedanceStdDev": 6.027456183070403,
                        "coefficientOfVariation": 1.4658129805029452,
                        "dispersion": 5.962133916683182
                    },
                    "characteristicFrequencies": {
                        "impedance10Hz": 5.637376656633329,
                        "impedance10000Hz": 2.3021358869347655
                    },
                    "cellColors": [
                        {
                            "cellId": "1",
                            "color": "#ffffff"
                        },
                        {
                            "cellId": "2",
                            "color": "#000000"
                        }
                    ]
                }
                """, PackMetricsResponse.class);
        return ResponseEntity.ok(response);
    }

    @Override
    @SneakyThrows
    public ResponseEntity<Overview> getDashboardOverview() {
        Overview overview = new ObjectMapper().registerModule(new JavaTimeModule()).readValue("""
                {
                    "totalMeasurements": 1,
                    "monthlyInspections": 0,
                    "cumulativeRiskIdentifications": 5,
                    "lastUpdatedAt": "2000-01-23T04:56:07.000+00:00",
                    "totalInspections": 6,
                    "alertTexts": [
                        "alertText1",
                        "alertText2"
                    ],
                    "totalRiskySamples": 5
                }
                """, Overview.class);
        return ResponseEntity.ok(overview);
    }
}
